package com.example.springboot_jxc_0511.jxc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.springboot_jxc_0511.jxc.common.Constants;
import com.example.springboot_jxc_0511.jxc.entity.Product;
import com.example.springboot_jxc_0511.jxc.entity.Sale;
import com.example.springboot_jxc_0511.jxc.entity.Users;
import com.example.springboot_jxc_0511.jxc.service.IProductService;
import com.example.springboot_jxc_0511.jxc.service.ISaleService;
import com.example.springboot_jxc_0511.jxc.service.impl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gongxl
 * @since 2021-05-11
 */
@Controller
@RequestMapping("/sale")
public class SaleController {
    @Autowired
    private IProductService iProductService;
    @Autowired
    private ISaleService iSaleService;

    /**
     * @return java.lang.String
     * @Author GongXl
     * @Description 销售模块
     * @Date 2021/5/11 16:59
     * @Param [model]
     **/
    @RequestMapping("/toMainXs")
    public String toMainXs(Model model) {
        List<Product> productList = iProductService.list();
        model.addAttribute("users", new Users());
        model.addAttribute("sale", new Sale());
        model.addAttribute("productList", productList);
        model.addAttribute("pageFlag", "xs");
        return "main_xs";
    }

    /**
     * @return java.lang.String
     * @Author GongXl
     * @Description 销售列表
     * @Date 2021/5/11 16:59
     * @Param [model]
     **/
    @RequestMapping("/toMainXsList")
    public String toMainXsList(@RequestParam(value = "current", defaultValue = "1") Long current, @RequestParam(value = "size", defaultValue = "5") Long size, String orderStr, Model model) {
//        List<Sale> saleList = iSaleService.list();
        Page page = new Page(current, size);
        List<OrderItem> orders = OrderItem.ascs(orderStr);
        page.setOrders(orders);
        Sale saleArg = new Sale();
        List<Sale> saleListByPage = iSaleService.getSaleListByPage(page, saleArg);
        page.setRecords(saleListByPage);
        model.addAttribute("users", new Users());
        model.addAttribute("pageFlag", "xs_list");
        model.addAttribute("saleList", saleListByPage);
        model.addAttribute("pageInfo", page);
        model.addAttribute("orderStr", orderStr);
        return "main_xs_list";
    }

    /**
     * @return java.lang.String
     * @Author GongXl
     * @Description 销售库存
     * @Date 2021/5/11 16:59
     * @Param [model]
     **/

    @RequestMapping("/toMainXsKc")
    public String toMainXsKc(Model model) {
        List<Product> productList = iProductService.list();
        model.addAttribute("users", new Users());
        model.addAttribute("sale", new Sale());
        model.addAttribute("productList", productList);
        model.addAttribute("pageFlag", "ks");
        return "yang.html";
    }

    /**
     * @param model
     * @return
     */
    @RequestMapping("/addSale")
    @Transactional
    public String addSale(Sale sale, Model model, HttpSession session) {
        if (null != sale) {
            sale.setTotalprice(iSaleService.getQuantity(sale));
            Object userObj = session.getAttribute(Constants.USER_INFO);
            if (null != userObj) {
                sale.setUserid(((Users) userObj).getId());
            }
            sale.setSaledate(new Date());
            boolean save = iSaleService.save(sale);
            //获得产品并更新库存
//            int i= 3/0;
//            pro.setQuantity();
            //更新库存，mq
            Product proTemp = iProductService.getById(sale.getProductid());
            Integer lastQuant = proTemp.getQuantity() - sale.getQuantity();//剩余产品数量
            if (lastQuant < 0) {
                model.addAttribute(Constants.ERROR_INFO, proTemp.getProductname() + "库存量不足，剩余" + proTemp.getQuantity());
                return toMainXs(model);
            }
            proTemp.setQuantity(lastQuant);
            boolean b = iProductService.updateById(proTemp);
        } else {
            model.addAttribute(Constants.ERROR_INFO, "保存失败");
        }
        return "redirect:/sale/toMainXsList";
    }
}

